<%@ Page language="vb" CodeFile="settingsBrand.aspx.vb" Inherits="settingsBrand_aspx_vb" %>
<%
    rowNumber = 0
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        brandID = Request.QueryString("brandID")
%>
<script type="text/javascript">
  function confirmSubmit() { 
	if (window.confirm('Are you sure you want to delete this car make?')) {
		location.href='settingsBrand.aspx?do=delete&brandID=<%= brandID %>';
		return false;
	}
	else {
		return false;
	}
  }
  </script>
<%
        If Len(brandID) > 0 Then 
            brandID = CLng(brandID)
            dbOpen()
            RecSet = Conn.Execute("Select brand, (Select Count(vehicleID) From tblVehicle Where brandID = " & brandID & ") As vehicleCount From tblBrand Where brandID = " & brandID & "")
            If RecSet.EOF Then 
                brandID = ""
            Else
                dbBrand = RecSet.Fields("brand").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End IF
        Over()
        strJS = ""
        strJS = strJS + "[""brand"", ""==''""]"
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>CAR MAKE SETTINGS<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='settingsBrand.aspx?do=save&brandID=" & brandID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Brand:</td><td align='right'><input type='text' name='brand' id='brand' size='40' maxlength='50' value='" & dbBrand & "'></td><td><div id='error_Brand' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td align='right' colspan='2'>")
        If CInt(dbVehicleCount) = 0 And CDbl(fragusLeasingUserTypeID) = 1 And Len(brandID) > 0 Then 
            Response.Write("<input type='button' value='Delete make' onClick='return confirmSubmit()'>&nbsp; ")
        End IF
        Response.Write("<input type='submit' value='Save'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>EXISTING CAR MAKES<hr class='hrHeadline'></td></tr>")
        dbOpen()
        RecSet = Conn.Execute("Select b.brandID, b.brand from tblBrand b Order by b.brand")
        If RecSet.EOF Then 
            Response.Write("<tr><td><font color='red'>No car makes found.</font></td></tr>")
        Else
            Do Until RecSet.EOF
                brandID = RecSet.Fields("brandID").Value
                brand = RecSet.Fields("brand").Value
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td><a href='settingsBrand.aspx?brandID=" & brandID & "'>" & brand & "</a></td>")
                Response.Write("<td algin='right'><a href='settingsModel.aspx?brandID=" & brandID & "'>Edit car models</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        brand = Request.Form("brand")
        brandID = Request.QueryString("brandID")
        If Len(brand) > 0 Then 
            brand = Trim(brand)
            brand = Replace(CStr(brand), "'", "")
            brand = Replace(CStr(brand), """", "")
            brand = Replace(CStr(brand), "<", "")
            brand = Replace(CStr(brand), ">", "")
            brand = Replace(CStr(brand), "=", "")
        End IF
        If Len(brandID) > 0 Then 
            brandID = CLng(brandID)
            dbOpen()
            Conn.Execute("Update tblBrand Set brand = '" & brand & "' Where brandID = " & brandID & "")
            dbClose()
        Else
            dbOpen()
            Conn.Execute("Insert Into tblBrand (brand) Values('" & brand & "')")
            dbClose()
        End IF
        Response.Redirect("settingsBrand.aspx?subDo=%20-%20SAVED")
    ElseIf Request.QueryString("do") = "delete" Then 
        brandID = Request.QueryString("brandID")
        brandID = CLng(brandID)
        dbVehicleCount = 1
        dbOpen()
        RecSet = Conn.Execute("Select Count(brandID) As vehicleCount From tblVehicle Where brandID = " & brandID & "")
        dbVehicleCount = RecSet.Fields("vehicleCount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If CInt(dbVehicleCount) = 0 And CDbl(fragusLeasingUserTypeID) = 1 Then 
            dbOpen()
            Conn.Execute("Delete From tblModel Where brandID = " & brandID & "")
            Conn.Execute("Delete From tblBrand Where brandID = " & brandID & "")
            dbClose()
        End IF
        Response.Redirect("settingsBrand.aspx?subDo=%20-%20DELETED")
    End IF
%>
